import pyautogui
import time
from datetime import datetime, timedelta

# 安全设置：当鼠标移到屏幕左上角(0,0)时停止程序
pyautogui.FAILSAFE = True

# 通道页标签项
CHANNEL_LABEL = (133,65)

# 通道选择框坐标
CHANNEL_CHECKBOXES = [
    (60, 435),
    (60, 595),
    (60, 755),
    (60, 915),
    (60, 1075)
]

# 开始时间输入框坐标
START_TIME = (266, 1625)

# 结束时间输入框坐标
END_TIME = (266, 1691)

# 导出数据按钮坐标
QUERY_BUTTON = (725, 1625)

# 时间段长度
TIME_DELTA = timedelta(minutes=5)

def activate_target_window(window_title):
    target_window = pyautogui.getWindowsWithTitle(window_title)[0]
    if not target_window.isMaximized:
        target_window.maximize()
    target_window.activate()

def main():
    # 激活并最大化目标窗口
    activate_target_window("H960GC_V1.4.2.3")
    time.sleep(0.5)

    # 点击通道标签页
    pyautogui.click(CHANNEL_LABEL[0], CHANNEL_LABEL[1])
    time.sleep(1)

    # 确定开始/结束时间
    end_time = datetime.now()
    start_time = end_time - TIME_DELTA

    # 输入开始时间
    pyautogui.click(START_TIME[0], START_TIME[1])
    pyautogui.write(start_time.strftime("%Y-%m-%d-%H-%M-%S-"))

    # 输入结束时间
    pyautogui.click(END_TIME[0], END_TIME[1])
    pyautogui.write(end_time.strftime("%Y-%m-%d-%H-%M-%S-"))

    # 导出数据
    for channel_checkbox in CHANNEL_CHECKBOXES:
        # 选中通道
        pyautogui.click(channel_checkbox[0], channel_checkbox[1])

        # 点击导出数据按钮
        pyautogui.click(QUERY_BUTTON[0], QUERY_BUTTON[1])
        time.sleep(1)

        # 关闭弹窗
        pyautogui.press('enter')

        pyautogui.click(channel_checkbox[0], channel_checkbox[1])


if __name__ == "__main__":
    main()
